<?php


/**
* CakePHP Routines Plugin
* http://cake-routines-plugin.googlecode.com
*
* @package cake.plugins.routines
* @subpackage cake.plugins.routines.controllers.components
* @author Ivan Chistyakov <zetruger@gmail.com>
* @version $Id: page_title.php 42 2010-05-12 12:31:22Z zetruger@gmail.com $
* @link $HeadURL: http://cake-routines-plugin.googlecode.com/svn/trunk/controllers/components/page_title.php $
* @license MIT License (http://opensource.org/licenses/mit-license.php)
*/


class PageTitleComponent extends Object {

var $Controller = NULL;

var $_defaults = array(
	'projectTitle' => NULL,
	'projectTitleSeparator' => ' - ',
	'pageTitle' => NULL,
	'pageSubtitle' => NULL,
	'pageCaption' => NULL,
	'dataCaptionGlue' => ' / ',
	'dataCaption' => NULL,
	'autoSetVars' => TRUE
);

var $settings = array();

function initialize(&$Controller, $settings = array()) {
	$this->Controller = &$Controller;
	$this->settings = $settings;
}

function _mergeSettings($settings = array()) {
	return array_intersect_key(Set::merge($this->_defaults, Configure::read('Components.PageTitle'), $this->settings, get_object_vars($this), $settings), $this->_defaults);
}

function setVarsForLayout($settings = array()) {
	$ctrl = &$this->Controller;
	extract($this->_mergeSettings($settings));
	// dataCaption
	if (is_array($dataCaption)) {
		$dataCaption = implode($dataCaptionGlue, $dataCaption);
	}
	// pageCaption
	if (empty($pageCaption)) {
		$string_id = $ctrl->params['controller'] . '_' . $ctrl->params['action'] . '_caption';
		$pageCaption = __($string_id, TRUE);
		if ($pageCaption === $string_id) {
			$pageCaption = '{data_caption}';
		}
	}
	$pageCaption = preg_replace('~\{data_caption\}~i', $dataCaption, $pageCaption);
	// pageSubtitle
	if (empty($pageSubtitle)) {
		$string_id = $ctrl->params['controller'] . '_' . $ctrl->params['action'] . '_subtitle';
		$pageSubtitle = __($string_id, TRUE);
		if ($pageSubtitle === $string_id) {
			$pageSubtitle = '{page_caption}';
		}
	}
	$pageSubtitle = preg_replace(array('~\{page_caption\}~i', '~\{data_caption\}~i'), array($pageCaption, $dataCaption), $pageSubtitle);
	// projectTitle
	if (empty($projectTitle)) {
		$projectTitle = __('project_title', TRUE);
		if ($projectTitle === 'project_title') {
			$projectTitle = Router::url('/', TRUE);
		}
	}
	// pageTitle
	if (empty($pageTitle)) {
		$pageTitle = __('page_title', TRUE);
		if ($pageTitle === 'page_title') {
			$string_id = $ctrl->params['controller'] . '_' . $ctrl->params['action'] . '_title';
			$pageTitle = __($string_id, TRUE);
			if ($pageTitle === $string_id) {
				$pageTitle = '{project_title}{pts}{page_subtitle}';
			}
		}
	}
	$pageTitle = preg_replace(array('~\{project_title\}~i', '~\{pts\}~i', '~\{page_subtitle\}~i', '~\{page_caption\}~i', '~\{data_caption\}~i'), array($projectTitle, $projectTitleSeparator, $pageSubtitle, $pageCaption, $dataCaption), $pageTitle);
	if ((bool)preg_match('~^1\.[0-2]\.~', Configure::version())) { // <= cake 1.2
		$ctrl->pageTitle = h($pageTitle);
	}
	$ctrl->set('project_title', h($projectTitle));
	$ctrl->set('title_for_layout', h($pageTitle));
	$ctrl->set('subtitle_for_layout', h($pageSubtitle));
	$ctrl->set('caption_for_layout', h($pageCaption));
	$ctrl->set('data_caption', h($dataCaption));
}

function beforeRender(&$Controller) {
	extract($this->_mergeSettings());
	if ($autoSetVars) {
		$this->setVarsForLayout();
	}
}

}


?>